package com.pencilisland.pay.mgt.console.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pencilisland.pay.mgt.console.bean.JstreeNode;
import com.pencilisland.pay.mgt.console.entity.SysMenu;
import com.pencilisland.pay.mgt.console.service.MenuService;

/**
 * 菜单处理器
 * 
 * @author Breeze
 *
 */
@Controller
@RequestMapping(value = "/console/menu")
public class MenuController {
	
	@Autowired
	MenuService menuService;
	
	/**
	 * 进入菜单管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "")
	public String toMenuTree() {
		return "console/menu";
	}
	
	/**
	 * 组织机构树
	 * 
	 * @param rootId
	 * @return
	 */
	@RequestMapping(value = "/tree/data")
	@ResponseBody
	public JstreeNode menuTreeData(@RequestParam(value = "rootId", required = false, defaultValue = "1") Long rootId) {
		return menuService.menuTreeData(rootId);
	}

	/**
	 * 保存更新
	 * 
	 * @param org
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public SysMenu save(@ModelAttribute SysMenu menu) {
		return menuService.save(menu);
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load/{id}")
	@ResponseBody
	public Map<String, Object> load(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", menuService.loadData(id));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			menuService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/session")
	@ResponseBody
	public List<SysMenu> menuSession() {
		return menuService.menuSession();
	}
	
	
}
